package tree.tree2.symmetry.maxAndMinTree;

import sun.reflect.generics.tree.Tree;
import tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @ClassName MinDepth
 * @Description TODO
 * @Author lenovo
 * @Date 2023-04-27 11:38
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class MinDepth {
    /**
     * 给定一个二叉树，找出其最小深度。
     * <p>
     * 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
     * <p>
     * 说明：叶子节点是指没有子节点的节点。
     *
     * @param root
     * @return
     */
    public int minDepth(TreeNode root) {
        if (root == null) return 0;

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int res = 1;

        while (!queue.isEmpty()) {
            int size = queue.size();

            while (size > 0) {
                TreeNode poll = queue.poll();
                if (poll.left == null && poll.right == null) {
                    return res;
                }
                if (poll.left != null) {
                    queue.add(poll.left);
                }
                if(poll.right!=null){
                    queue.add(poll.right);
                }

                size--;
            }
            res++;
        }
        return res;
    }

    public static void main(String[] args) {
        MinDepth minDepth = new MinDepth();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        System.out.println(minDepth.minDepth(root));
    }
}